﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using ShopSpace.Core;

namespace ShopSpace.UI.DotNetNukeModules.ProjectManagement
{
	public partial class EditWorkItem : EditModuleControlBase
	{
		private Project project;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			try
			{
				this.InitParameters();
			}
			catch (Exception ex)
			{
				this.HandleException(ex);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				this.SetupLinks();
				if (!this.Page.IsPostBack)
				{
					this.SetupForm();
					this.BindWorkItem();
				}
			}
			catch (Exception ex)
			{
				this.HandleException(ex);
			}
		}

		protected void lnkUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				this.SaveWorkItem();
				Response.Redirect(Globals.NavigateURL());
			}
			catch (Exception ex)
			{
				this.HandleException(ex);
			}
		}

		private void InitParameters()
		{
			int projectId;
			if (!int.TryParse(Request["ProjectId"], out projectId))
			{
				throw new Exception("ProjectId parameter was null");
			}
			this.project = this.ProjectRepository.Get(projectId);
		}

		private void SetupLinks()
		{
			this.hlCancel.NavigateUrl = Globals.NavigateURL();
		}

		private void SetupForm()
		{
			this.ddlAssignedTo.Items.Clear();
			this.ddlAssignedTo.Items.Add(new ListItem("Not Assigned", "0"));
			foreach (ProjectPerson projectPerson in this.ProjectPersonRepository.ListAssignableToProject(this.project))
			{
				this.ddlAssignedTo.Items.Add(new ListItem(projectPerson.Person.DisplayName, projectPerson.Person.Id.ToString()));
			}

			this.ddlMilestone.DataSource = this.MilestoneRepository.ListByProject(this.project);
			this.ddlMilestone.DataBind();
			this.ddlMilestone.Items.Insert(0, (new ListItem("Not Assigned", "0")));

			foreach (string name in Enum.GetNames(typeof(Priority)))
			{
				this.ddlPriority.Items.Add(name);
			}

			this.ddlCategory.DataSource = this.WorkItemCategoryRepository.List();
			this.ddlCategory.DataBind();
			this.ddlCategory.Items.Insert(0, new ListItem("Select One", "0"));

			this.ddlStatus.DataSource = this.WorkItemStatusRepository.List();
			this.ddlStatus.DataBind();
		}

		private void BindWorkItem()
		{
			if (this.editItemId.HasValue)
			{
				WorkItem workItem = this.WorkItemRepository.Get(this.editItemId.Value);
				this.lblWorkItem.Text = workItem.Summary;
				this.ItemConcurrencyDate = workItem.ConcurrencyDate;
				this.lblReportedBy.Text = workItem.ReportedBy.DisplayName;
				this.txtSummary.Text = workItem.Summary;
				this.txtDescription.Text = workItem.Description;
				this.ddlCategory.SelectedValue = workItem.Category.Id.ToString();
				this.ddlPriority.SelectedValue = workItem.Priority.ToString();
				this.ddlStatus.SelectedValue = workItem.Status.Id.ToString();
				this.ddlAssignedTo.SelectedValue = workItem.AssignedTo.Id.ToString();
				this.ddlMilestone.SelectedValue = workItem.Milestone.Id.ToString();
			}
			else
			{
				this.lblWorkItem.Text = "New Work Item";
				this.lblReportedBy.Text = this.Person.DisplayName;
				this.ddlPriority.SelectedValue = Priority.Normal.ToString();
				this.ddlStatus.Enabled = false;
				this.ddlMilestone.Enabled = false;
				this.ddlAssignedTo.Enabled = false;
			}
		}

		private void SaveWorkItem()
		{
			WorkItem workItem;
			if (this.editItemId.HasValue)
			{
				workItem = this.WorkItemRepository.Get(this.editItemId.Value);
				workItem.ConcurrencyDate = this.ItemConcurrencyDate;
				workItem.Summary = this.txtSummary.Text.Trim();
			}
			else
			{
				workItem = new WorkItem(this.project, this.Person, this.txtSummary.Text.Trim());
			}
			workItem.Category = this.WorkItemCategoryRepository.Get(int.Parse(this.ddlCategory.SelectedValue));
			workItem.Description = this.txtDescription.Text.Trim();
			workItem.ChangeStatus(this.WorkItemStatusRepository.Get(int.Parse(this.ddlStatus.SelectedValue)), this.Person);
			workItem.ChangePriority((Priority)Enum.Parse(typeof(Priority), this.ddlPriority.SelectedValue), this.Person);

			Person assignedTo = Person.NOT_ASSIGNED;
			int assignToId = int.Parse(this.ddlAssignedTo.SelectedValue);
			if (assignToId != 0)
			{
				assignedTo = this.PersonRepository.Get(assignToId);
			}
			workItem.AssignedTo = assignedTo;

			Milestone milestone = Milestone.NOT_ASSIGNED;
			int milestoneId = int.Parse(this.ddlMilestone.SelectedValue);
			if (milestoneId != 0)
			{
				milestone = this.MilestoneRepository.Get(milestoneId);
			}
			workItem.ChangeMilestone(milestone, this.Person);

			if (!this.editItemId.HasValue)
			{
				this.WorkItemRepository.Add(workItem, this.UserInfo.Username);
			}
			else
			{
				this.WorkItemRepository.Update(workItem, this.UserInfo.Username);
			}
			this.UserState.AddMessage(this.ModuleId, string.Format("{0} was {1}", workItem.Summary, this.editItemId.HasValue ? "updated" : "added"), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
		}
	}
}